﻿

function show(id)
{
   $('#' + id + 'Canvas').css('display', 'none');
}
		   
function hide(id)
{
   $('#' + id + 'Canvas').css('display', 'block');
}
		   

$(function(){
	function LoadCommentBoard()
	{
		//hide('cb_container');
		$('#cb_containerCanvas').hide('slow');

		$.ajax({
		  url: '/commentboard/display/?id=' + boardId,
		  type: 'GET',
		  dataType: 'text',
		  timeout: 10000,
		  error: function(){
			  alert('Error loading XML document');
			  //show('cb_container');
			  $('#cb_containerCanvas').show('slow');
		  },
		  success: function(xml){
     		  show('cb_container');
			  var commentboard = $('.cb_commentboard');
			  
			  if ($(xml).find('.comment').size() > 0)
			  {
				 var currentCommentsNumber = commentboard.find('.cb_comment').size();
				 if (currentCommentsNumber == 0)
				 {
					 commentboard.empty();
				 }
				 var i = 0;
				$(xml).find('.comment').each(
				  function() {
					  ++i;
					  if (i <= currentCommentsNumber)
					  {
						  return;
					  }
					  var comment = $('<div class="cb_comment"><div class="cb_info"><div class="cb_author"></div><div class="cb_date"></div></div><div class="cb_content"></div></div>');
					  comment.find('.cb_author').append($(this).find('.username').text() + '  说:');
					  comment.find('.cb_content').append($(this).find('.content').text());
					  comment.find('.cb_date').append($(this).find('.comment_date').text());				
					  comment.hover(
						function() {
							$(this).css('background-color', '#DDDCD9');
						},
						function() {
							$(this).css('background-color', '#FFF');
						}
					  )
					  comment.appendTo(commentboard).hide().slideDown(500)	;
				  }
				)
			  }
			  else
			  {
  			      commentboard.empty();
				  commentboard.append('还没有任何评论，请添加评论');
			  }
				
			 
		  }
		});
	}
		   
		   
	$('#submit').click(
		function(){
			//validation.
			 if ($('#cb_commentText').val().trim().length == 0)
			 {
				 alert('请写点什么吧！');
				 return;
			 }
			
			hide('cb_container');
			$.ajax({
			  url: '/commentboard/addcomment/',
			  type: 'POST',
			  data: 'board_id=' + boardId + '&content=' + $('#cb_commentText').val(),
			  dataType: 'text',
			  timeout: 10000,
			  error: function(){
				  alert('Error loading XML document');
				  show('cb_container');
			  },
			  success: function(xml){
				  $('#cb_commentText').val('');
				  LoadCommentBoard();
			  }
		   })
		}
	)
	
	$('#cb_updateBoard').click(
		function() {
			LoadCommentBoard();
		}
	);
	
	
	LoadCommentBoard();
		 

	

})